1 module hip.api.assets.assets_binding;
2 public import hip.api.data.commons;
3 public import hip.api.data.font;
4 public import hip.api.data.textureatlas;
5 public import hip.api.data.tilemap;
6 public import hip.api.renderer.texture;
7 
8 version(ScriptAPI)
9 {
10     void initAssetManager()
11     {
12         import hip.api.internal;
13         loadClassFunctionPointers!(HipAssetsBinding, UseExportedClass.Yes, "HipAssetManager");
14         import hip.api.console;
15         log("HipengineAPI: Initialized AssetManager");
16     }
17 
18 
19     class HipAssetsBinding
20     {
21         extern(System) __gshared
22         {
23             ///Returns whether asset manager is loading anything
24             bool function() isLoading;
25             ///Stops the code from running and awaits asset manager to finish loading
26             void function() awaitLoad;
27 
28 
29             
30             /** 
31             * Used for manually creating texture regions. This is used by the game code abstraction.
32             */
33             IHipTextureRegion function(IHipTexture texture, float u1 = 0, float v1 = 0, float u2 = 1, float v2 = 1) createTextureRegion;
34             
35             /** 
36             * Used for creating procedurally generated Tilemap:
37             ```d
38             IHipTilemap map = HipAssetManager.createTilemap(200, 200, 1, 1);
39             map.addTileset(HipAssetManager.tilesetFromSpritesheet(mySpritesheet));
40             HipTileLayer layer = map.addNewLayer("MyLayer", 200, 200);
41             //Define your tiles by accessing layer.tiles = []
42             ```
43             */
44             IHipTilemap function(uint width, uint height, uint tileWidth, uint tileHeight, bool isInfinite = false) createTilemap;
45 
46             
47             /**
48             *   Reserved for deferred loading.
49             *   Use it on your own risk.
50             */
51             void function (IHipAssetLoadTask task, void delegate(IHipAsset) onComplete) addOnCompleteHandler;
52 
53             IHipAsset function(string name) getAsset;
54             string function(string name) getStringAsset;
55 
56             ///File reading wrapped in asset manager.
57             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadFile;
58 
59             ///Loads an in memory audio clip
60             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadAudio;
61 
62             ///Returns a load task for texture
63             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadTexture;
64             ///Returns a load task for image
65             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadImage;
66             ///Returns a load task for tilemap
67             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadTilemap;
68 
69             /**
70             *   This function is used in conjunction usually with `createTilemap`.
71             *   `loadTileset` is a way of loading an externally defined tileset for your procedural map.
72             *   Use `loadTilemap` when you have a complete map which you wish to load.
73             */
74             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadTileset;
75 
76 
77             /**
78             *   Usage:
79             ```d
80             //Iterate every value
81             foreach(v; csv) //or
82             //Iterate columns
83             foreach(v; csv.getColumnRange(0)) //or
84             //Iterate rows
85             foreach(v; csv.getRow(0))
86             //Get the csv cell
87             csv[x, y]
88             ```
89             * Returns: IHipCSV
90             */
91             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadCSV;
92             /**
93             *   Usage:
94             ```d
95             //If not found, use 2 as default
96             ini.tryGet!ubyte("buffering.count", 2);
97             //Alternative usage
98             ini.buffering.count.get!ubyte 
99             ```
100             * Returns: IHipINI
101             */
102             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadINI;
103             /**
104             *   Usage:
105             ```d
106             //Must import hip.data.json for actually using it.
107             import hip.data.json;
108             JSONValue json = hipJSON.getJSON!JSONValue;
109             json["myProperty"].str//or other types
110             ```
111             * Returns: IHipJSONC
112             */
113             IHipAssetLoadTask function(string path, string f = __FILE__, size_t l = __LINE__) loadJSONC;
114 
115             
116             /** 
117             *   Returns a load task for a texture atlas
118             *   If ":IGNORE" is provided for texturePath, the following behavior will occur:
119             *   - .json: Will try to load a file with same name but with extension .png
120             *   - .atlas: texturePath is always ignored
121             *   - .txt(or any): Load a file with same name but extension .png
122             *   - .xml: Ignore internal texture path to try file with same name but .png extension
123             */
124             IHipAssetLoadTask function(string atlasPath, string texturePath = ":IGNORE", string f = __FILE__, size_t l = __LINE__) loadTextureAtlas;
125             // /Returns a load task for font, when used, 
126             IHipAssetLoadTask function(string path, int fontSize = 48, string f = __FILE__, size_t l = __LINE__) loadFont;
127 
128             version(Have_util)
129             {
130                 public import hip.util.data_structures:Array2D, Array2D_GC;
131                 IHipTileset function(Array2D_GC!IHipTextureRegion spritesheet) tilesetFromSpritesheet;
132             }   
133             IHipTileset function(IHipTextureAtlas atlas) tilesetFromAtlas;
134 
135             //TODO: IHipAssetLoadTask function(string path) loadHapFile;
136         }
137     }
138     import hip.api.internal;
139     mixin ExpandClassFunctionPointers!HipAssetsBinding;
140 }
141 
142 
143 
144 
145 T get(T)(string name){return cast(T)getAsset(name); }
146 T get(T : string)(string name){return cast(T)getStringAsset(name);}